package org.example.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.commonapi.domain.Course;
import org.example.commonapi.dto.CourseInfoDto;
import org.example.commonapi.dto.PageInfo;
import org.example.commonapi.dto.StudentDto;
import org.example.commonapi.mapper.UserMapper;
import org.example.course.service.CourseService;
import org.example.commonapi.mapper.CourseMapper;
import org.example.course.until.CourseUntil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static org.example.commonapi.domain.Constants.NUMBER;
import static org.example.commonapi.domain.Constants.STUDENT_ROLE;

/**
* @author DELL
* @description 针对表【course】的数据库操作Service实现
* @createDate 2024-06-01 21:55:22
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService {

    @Resource
    private CourseMapper courseMapper;


    /**
     * 查询我的课程
     * @param state
     * @param courseName
     * @param id
     * @return
     */
    @Override
    public PageInfo studentList(Integer pageNum,Integer pageSize,Integer state, String courseName, Long id) {
//        创建分页对象
        Page<CourseInfoDto> page = new Page<>(pageNum,pageSize);
//        设置分页参数
        IPage<CourseInfoDto> pages=courseMapper.studentList(page,state, courseName, id);
        return new PageInfo(pages);
    }

    /**
     * 查询所有课程
     * @param state
     * @param courseName
     * @return
     */
    @Override
    public List<CourseInfoDto> list(Integer state, String courseName) {
        return courseMapper.publicList(state, courseName);
    }

    /**
     * 添加课程
     * @param course
     * @param id
     */
    @Override
    public void insertCourse(Course course, Long id) {
//        设置基础信息
        course.setTeacherId(id);
        course.setNumber(NUMBER);
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        course.setInvite(CourseUntil.createInviteCode());
        courseMapper.insert(course);
    }

    /**
     * 查看教师列表
     * @param pageNum
     * @param pageSize
     * @param state
     * @param courseName
     * @param id
     * @return
     */
    @Override
    public PageInfo teacherList(Integer pageNum,Integer pageSize,Integer state, String courseName, Long id) {
        //        创建分页对象
        Page<CourseInfoDto> page = new Page<>(pageNum,pageSize);
//        设置分页参数
        IPage<CourseInfoDto> pages=courseMapper.teacherList(page,state, courseName, id);
        return new PageInfo(pages);
    }

    /**
     * 更新课程人数
     * @param courseId
     * @param number
     */
    @Override
    public void updateCourseNumber(Long courseId, Integer number) {
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",courseId).setSql("number=number+"+number);
        courseMapper.update(updateWrapper);
    }


}




